package com.zd.kids.ui.activity;

import android.util.Log;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.youth.banner.transformer.ScaleInTransformer;
import com.zd.kids.DataBean.TrainWordDataBean;
import com.zd.kids.R;
import com.zd.kids.common.Constants;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;
import com.zd.kids.ui.fragment.TrainWordFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.CompositePageTransformer;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;
import butterknife.BindView;


/**
 * 训练词语
 */
public class TrainWordActivity extends BaseActivity {

    public static final String WORD_ID_KEY = "WORD_ID_KEY";


    public static final String IS_SCAN_KEY = "IS_SCAN_KEY";

    @BindView(R.id.view_pager)
    ViewPager2 viewPager;

    private String mWordId;

    private boolean isScan;

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_train_word;
    }

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        initTitle("", R.color.cartoon_title_color);

        mWordId = getIntent().getStringExtra(WORD_ID_KEY);
        isScan = getIntent().getBooleanExtra(IS_SCAN_KEY, false);

        if (isScan) {
            getScanWords(mWordId);
        } else {
            getWordsData(mWordId);
        }


        viewPager.setClipChildren(false);
        initPage2();
    }

    private List<Fragment> datas;

    private FragmentAdapter mFragmentAdapter;

    private void initPage2() {


        datas = new ArrayList<>();
        mFragmentAdapter = new FragmentAdapter(this, datas);
        viewPager.setAdapter(mFragmentAdapter);

        CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
//
        compositePageTransformer.addTransformer(new MarginPageTransformer(0));
        compositePageTransformer.addTransformer(new ScaleInTransformer(0.90f));
        viewPager.setPageTransformer(compositePageTransformer);

        viewPager.setOffscreenPageLimit(3);

    }


    class FragmentAdapter extends FragmentStateAdapter {

        private List<Fragment> mDatas;

        public FragmentAdapter(@NonNull FragmentActivity fragmentActivity, List<Fragment> mDatas) {
            super(fragmentActivity);
            this.mDatas = mDatas;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return mDatas.get(position);
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }
    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }


    private void getWordsData(String unitId) {

        showLoading();
        HttpMethod.getIntence().getNewwordList(unitId, new CustomObserver<TrainWordDataBean>(this) {
            @Override
            public void onSuccess(TrainWordDataBean trainWordDataBean) {
                disLoading();
                datas.clear();
                tvTitle.setText(trainWordDataBean.getQuestion_name());
                List<TrainWordDataBean.ResultBean> itemDatas = trainWordDataBean.getResult();

                if (itemDatas != null) {

                    for (int i = 0; i < itemDatas.size(); i++) {
                        TrainWordDataBean.ResultBean resultBean = itemDatas.get(i);
                        TrainWordFragment fragment = TrainWordFragment.newInstance(resultBean);
                        fragment.setUnitId(unitId);
                        fragment.setEndWord(i == itemDatas.size() - 1);
                        datas.add(fragment);
                    }
                    mFragmentAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onError(String errorCode) {
                disLoading();
            }

        });
    }


    private void getScanWords(String newwordid) {

        showLoading();
        HttpMethod.getIntence().getScanWords(newwordid, new CustomObserver<TrainWordDataBean>(this) {
            @Override
            public void onSuccess(TrainWordDataBean trainWordDataBean) {

                disLoading();
                tvTitle.setText(trainWordDataBean.getQuestion_name());
                datas.clear();

                List<TrainWordDataBean.ResultBean> itemDatas = trainWordDataBean.getResult();
                if (itemDatas != null) {
                    for (int i = 0; i < itemDatas.size(); i++) {

                        TrainWordDataBean.ResultBean resultBean = itemDatas.get(i);
                        TrainWordFragment fragment = TrainWordFragment.newInstance(resultBean);
                        datas.add(fragment);

                        if (newwordid.equals(resultBean.getNewwordid() + "")) {
                            viewPager.setCurrentItem(i, false);
                        }
                    }
                    mFragmentAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onError(String errorCode) {
                Log.i("ScanActivity", "errorCode:" + errorCode);
                disLoading();
            }

        });
    }

    @Override
    public void finish() {
        super.finish();

        FileUtils.deleteAllInDir(Constants.RECORD_ROOT_PATH);
    }
}
